package com.lvmama.dashboard.utils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lvmama.com.lvtu.common.utils.UUIDGenerator;
import com.lvmama.dashboard.entity.UserUser;
import com.lvmama.java.rhino.spark.utils.Constants;
import com.lvmama.rhino.utils.JedisClusterUtils;

/**
 * @author wxliyong
 * @date 2016-8-6 23:58:18
 */
public class ServletUtils {
//	private static final Logger logger = Logger.getLogger(ServletUtils.class);
	/**
	 * 获取当前的回话ID
	 * @param request
	 * @param response
	 * @return
	 */
	public static String getLogsessionid(HttpServletRequest request, HttpServletResponse response) {
		String logsessionid = request.getParameter(Constants.LOGSESSIONID);
		if(logsessionid != null && !logsessionid.trim().equals("")) {
			return logsessionid;
		}
		Cookie[] cookies = request.getCookies();
		if(cookies != null) {
			for(Cookie cookie : cookies) {
				if(cookie.getName().equals(Constants.LOGSESSIONID)) {
					logsessionid = cookie.getValue();
					request.setAttribute(Constants.LOGSESSIONID, logsessionid);
					return logsessionid;
				}
			}
		}
		logsessionid = (String) request.getAttribute(Constants.LOGSESSIONID);
		if(logsessionid != null && !"".equals(logsessionid)) {
			return logsessionid;
		}
		logsessionid = new UUIDGenerator().generate().toString();
		Cookie cookie = new Cookie(Constants.LOGSESSIONID, logsessionid);
		cookie.setDomain(Constants.DOMAIN);
		cookie.setPath("/");
		cookie.setMaxAge(-1);
		response.addCookie(cookie);
		request.setAttribute(Constants.LOGSESSIONID, logsessionid);
		return logsessionid;
	}
	
	/**
	 * 从回话中获取用户信息
	 * @param logsessionid
	 * @return
	 */
	public static UserUser getUser(String logsessionid) {
		return getDataFromSession(logsessionid, Constants.SESSION_USER_KEY, UserUser.class);
	}
	
	/**
	 * 将数据放入回话中
	 * @param logsessionid 回话ID
	 * @param key
	 * @param value
	 */
	public static void putSession(String logsessionid, String key, Object value) {
		Map<String, Object> sessionMap = JedisClusterUtils.getInstance().hmget(logsessionid);
		if(sessionMap == null) {
			sessionMap = new HashMap<String, Object>();
		}
		sessionMap.put(key, value);
		sessionMap.put(Constants.SESSION_LAST_UPDATE_TIME, new Date());
		JedisClusterUtils.getInstance().hmset(logsessionid, sessionMap, Constants.SESSION_TIMEOUT * 1000);
	}
	
	
	/**
	 * 将指定的key从回话中移除
	 * @param logsessionid
	 * @param key
	 */
	public static void removeFromSession(String logsessionid, String key) {
		Map<String, Object> sessionMap = JedisClusterUtils.getInstance().hmget(logsessionid);
		if(sessionMap == null) {
			return;
		}
		sessionMap.put(Constants.SESSION_LAST_UPDATE_TIME, new Date());
		sessionMap.remove(key);
		JedisClusterUtils.getInstance().hmset(logsessionid, sessionMap, Constants.SESSION_TIMEOUT * 1000);
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T getDataFromSession(String logsessionid, String key, Class<T> returnClass) {
		Map<String, Object> sessionMap = JedisClusterUtils.getInstance().hmget(logsessionid);
		if(sessionMap == null) {
			return null;
		}
		Object obj = sessionMap.get(key);
		if(obj == null || !(obj instanceof UserUser)) {
			return null;
		}
		JedisClusterUtils.getInstance().hmset(logsessionid, sessionMap, Constants.SESSION_TIMEOUT * 1000);
		return (T)obj;
	}
	
	public static String getBaseUrl(HttpServletRequest request, HttpServletResponse response) {
		StringBuffer sb = new StringBuffer("http://");
		sb.append(request.getServerName());
		if(80 != request.getServerPort()) {
			sb.append(request.getServerPort());
		}
		sb.append(request.getContextPath());
		return sb.toString();
	}
}
